package com.yc.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.yc.bean.Good;
import com.yc.bean.Types;
import com.yc.bean.User;
import com.yc.biz.GoodBiz;
import com.yc.biz.TypesBiz;
import com.yc.biz.UserBiz;
import com.yc.biz.impl.GoodBizImpl;
import com.yc.biz.impl.TypesBizImpl;
import com.yc.biz.impl.UserBizImp1;

/**
 * @author Huathy
 * 这个类包含所有与管理员端相关的请求操作
 */
@WebServlet("/admin_good")
public class Admin_GoodsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	GoodBiz gb = new GoodBizImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String op = req.getParameter("op");
		PrintWriter out = resp.getWriter();
		if( "getGoodsbyPage".equals(op) ){
			getGoodsbyPage(req,resp,out);
		}else if ("getGoodsByTypePage".equals(op)) {
			getGoodsByTypePage(req, resp, out);
		}else if ("good_del".equals(op)) {
			good_del(req, resp, out);
		}else if ("good_add".equals(op)) {
			good_add(req, resp, out);
		}else if ("good_modify".equals(op)) {
			good_modify(req, resp, out);
		}
	}
	
	
	private void good_modify(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int gid = 0;
		int tid = 0;
		String gname = null;
		String pic = null;
		int inventory = 0;
		double price = 0;
		String color = null;
		String version = null;
		String message = null;
		
		try {
			gid = Integer.parseInt( req.getParameter("gid") );
			tid = Integer.parseInt( req.getParameter("tid") );
			gname = req.getParameter("gname");
			if(req.getSession().getAttribute("pic")==null){
				pic = req.getParameter("pic");
			}else{
				pic = (String) req.getSession().getAttribute("pic");
			}
			inventory = Integer.parseInt( req.getParameter("inventory") );
			price = Double.parseDouble( req.getParameter("price") );
			color = req.getParameter("color");
			version = req.getParameter("version");
			message = req.getParameter("message");
		} catch (Exception e) {
			e.printStackTrace();
			out.print(-1);
		}
		int result = gb.good_modify(gid,tid,gname,pic,inventory,price,color,version,message);
		out.print(result);
//		out.print(1);
	}

	private void good_add(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int tid = 0;
		String tname = null;
		int inventory = 0;
		double price = 0;
		String color = null;
		String version = null;
		String message = null;
		String pic = null;
		try {
			tid = Integer.parseInt( req.getParameter("tid") );
			tname = req.getParameter("gname");
			pic = 	(String) req.getSession().getAttribute("pic");
			inventory = Integer.parseInt( req.getParameter("inventory") );
			price = Double.parseDouble( req.getParameter("price") );
			color = req.getParameter("color");
			version = req.getParameter("version");
			message = req.getParameter("message");
			pic = (String) req.getSession().getAttribute("picUrl");
		} catch (Exception e) {
			e.printStackTrace();
			out.print(-1);
		}
		int result = gb.good_add(tid,tname,pic,price,color,version,message,inventory);
		out.print(result);
	}

	private void good_del(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int gid = -1;
		try {
			gid = Integer.parseInt(req.getParameter("gid"));
		} catch (NumberFormatException e) {
//			e.printStackTrace();
			out.print(-1);
			return;
		}
		int result = gb.good_del(gid);
		out.print(result);
	}

	/**
	 * 根据类型页面查询商品
	 * @param req
	 * @param resp
	 * @param out
	 */
	private void getGoodsByTypePage(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int type = 0;
		int page = 1;
		try {
			type = Integer.parseInt( req.getParameter("type") );
			page = Integer.parseInt( req.getParameter("page") );
		} catch (NumberFormatException e) {
//			e.printStackTrace();
		}
		
	}

	//通过页面获取商品
	private void getGoodsbyPage(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int totalPage = 0;
		try {
			totalPage = Integer.parseInt( req.getParameter("goodsTotalPage") );
		} catch (NumberFormatException e1) {
//			e1.printStackTrace();
			totalPage = gb.getGoodsTotalPage();
			req.getSession().setAttribute("goodsTotalPage", totalPage);
		}
		
		int page = 1;
		try {
			page = Integer.parseInt( req.getParameter("page") );
		} catch (Exception e) {
//			e.printStackTrace();
		}
		if( page<=0 ){
			page=1;		//page不可为0
		}
		if( page>totalPage ){
			page = totalPage;
		}
		List<Good> goods = gb.getGoodsByPage(page);
		req.getSession().setAttribute("goods",goods);
		if(goods.isEmpty()){
			out.print(-1);
		}
		out.print(page);
	}

}
